namespace Plugins.Serializer
{
    using System;
    using System.ComponentModel;
    using System.IO;
    using System.Security;
    using Plugins.Interfaces;
    using Plugins.Library;

    [Serializable]
    public abstract class PluginFileStreamSerializer<T> : PluginStreamSerializer<T>, 
        IPluginFileSerializer<T>, 
        IPluginSerializer<T>
    {
        protected abstract string GetFileName(string fileName);
        protected abstract bool Deserialize(string fileName, IPluginTypeFormatter formatter);
        protected abstract bool Serialize(string fileName, T obj, IPluginTypeFormatter formatter);

        bool IPluginFileSerializer<T>.Deserialize(string fileName)
        {
            lock (((PluginFileStreamSerializer<T>)this))
            {
                return this.Deserialize(fileName, XmlTypeFormatter);
            }
        }

        bool IPluginFileSerializer<T>.Deserialize(string fileName, IPluginRuntimeFormatter formatter)
        {
            lock (((PluginFileStreamSerializer<T>) this))
            {
                try
                {
                    return this.Deserialize(fileName, formatter);
                }
                catch (FileLoadException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (FileNotFoundException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (IOException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (ArgumentNullException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (SecurityException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (AccessViolationException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (ApplicationException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (FormatException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (ArgumentException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (UnauthorizedAccessException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (InvalidOperationException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (Win32Exception ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                return false;
            }
        }

        bool IPluginFileSerializer<T>.Serialize(string fileName, T obj)
        {
            lock (((PluginFileStreamSerializer<T>) this))
            {
                return this.Serialize(fileName, obj, XmlTypeFormatter);
            }
        }

        bool IPluginFileSerializer<T>.Serialize(string fileName, T obj, IPluginRuntimeFormatter formatter)
        {
            lock (((PluginFileStreamSerializer<T>) this))
            {
                try
                {
                    return this.Serialize(fileName, obj, formatter);
                }
                catch (FileLoadException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (FileNotFoundException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (IOException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (ArgumentNullException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (SecurityException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (AccessViolationException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (ApplicationException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (FormatException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (ArgumentException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (UnauthorizedAccessException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (InvalidOperationException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (Win32Exception ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                return false;
            }
        }
    }
}

